package com.zhaocheng.tank;

import java.awt.*;
import java.util.Random;

/**
 * @author zhaocheng
 * @version 1.0
 * @date 2020/6/21 0021 9:58
 */
public class Tank {
    public int x;
    public int y;
    private Dir dir = Dir.DOWN;
    private boolean moveing = true;
    private TankFrame tf = null;
    private Group group = Group.GOOD;
    private Random random = new Random();
    Rectangle rect = new Rectangle();

    private boolean living = true;

    private static final int SPEED = 5;
    public static final int WIDHT = ResourceManage.goodTankU.getWidth();
    public static final int HIGHT = ResourceManage.goodTankU.getHeight();

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public boolean isLiving() {
        return living;
    }

    public void setLiving(boolean living) {
        this.living = living;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoveing() {
        return moveing;
    }

    public void setMoveing(boolean moveing) {
        this.moveing = moveing;
    }

    public Tank(int x, int y, Dir dir, TankFrame tf, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDHT;
        rect.height = HIGHT;
    }

    public void paint(Graphics g) {
        if (!living) {
            tf.bedTank.remove(this);
            return;
        }
        switch (dir) {
            case RIGHT:
                g.drawImage(group == Group.GOOD ? ResourceManage.goodTankR : ResourceManage.bedTankR, x, y, null);
                break;
            case UP:
                g.drawImage(group == Group.GOOD ? ResourceManage.goodTankU : ResourceManage.bedTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(group == Group.GOOD ? ResourceManage.goodTankD : ResourceManage.bedTankD, x, y, null);
                break;
            case LEFT:
                g.drawImage(group == Group.GOOD ? ResourceManage.goodTankL : ResourceManage.bedTankL, x, y, null);
                break;
            default:
                break;
        }
        move();
    }

    private void move() {
        if (!moveing) {
            return;
        }
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }
        if (group == Group.BED && random.nextInt(100) > 95) {
            this.fire();
        }
        if (group == Group.BED && random.nextInt(100) > 95) {
            randomDir();
        }
        boundsCheck();
        rect.x = this.x;
        rect.y = this.y;
        if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
            this.living = false;
        }

    }

    /**
     * 边际检测
     */
    private void boundsCheck() {
        if (this.x < 2) {
            this.x = 2;
        }
        if (this.y < 28) {
            this.y = 28;
        }
        if (this.x > TankFrame.GAME_WIDTH - WIDHT - 2) {
            this.x = TankFrame.GAME_WIDTH - WIDHT - 2;
        }
        if (this.y > TankFrame.GAME_HEIGHT - HIGHT - 2) {
            this.y = TankFrame.GAME_HEIGHT - HIGHT - 2;
        }
    }

    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void fire() {

        int bX = x + Tank.WIDHT / 2 - Bullet.WIDTH / 2;
        int by = y + Tank.HIGHT / 2 - Bullet.HIGHT / 2;
        tf.bullets.add(new Bullet(bX, by, dir, tf, this.group));
    }

    public void die() {
        this.living = false;
    }
}
